<style scoped>
.layui-row {
    overflow: hidden;
}

.select-item {
    width: 34.333333%;
    float: right;
}

.layui-row:after,
.layui-row:before {
    clear: none;
}
</style>

<template>
<div class="department">
    <div>
        <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加库间</a>
    </div>
    <form class="layui-form">

        <div class="layui-col-lg4 select-item" style="text-align: right;width: auto">
            <div class="layui-input-inline" style="width: 200px">
                <select class="layui-select" lay-verify="required" id="sName" lay-filter="shr" style="width: 200px">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline" style="width: 200px">
                <select class="layui-select" lay-verify="required" id="shrtype" lay-filter="shrtype" style="width: 200px">
                    <option value="">请选择</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <button type="button" style="width: 100px" class="layui-btn sou" @click="search">搜索</button>
            </div>

        </div>

    </form>

    <table class="layui-table layui-form">

        <thead>
            <tr>
                <th>库房名称 </th>
                <th>库房编号</th>
                <th>库间名称</th>
                <th>库间编号</th>
                <th>库间类型</th>
                <th>容量</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr class="id9" v-for="(val, index) in departments">
                <td>{{val.storename}}</td>
                <td>{{val.storecode}}</td>
                <td>{{val.name}}</td>
                <td>{{val.code}}</td>
                <td v-for="(item,index) in shrtypes" v-if="item.dictkey == val.shrtype">{{item.dicttext}}</td>
                <td>{{val.capacity}}</td>
                <td>
<!--                    <a href="javascript:;" v-if="chaneType(val.storeguid,val.guid)!==false" @click="edit(val.guid)">修改</a>-->
<!--                    <a v-if="chaneType(val.storeguid,val.guid)!==false">|</a>-->
                    <a href="javascript:;" @click="edit(val.guid)">修改</a>
                    <a>|</a>
                    <a href="javascript:;" @click="del(val.guid,val.code)">删除</a>
                </td>
            </tr>
        </tbody>
    </table>

    <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage"></Pagination>
</div>
</template>

<script>
import Pagination from '../common/Pagination.vue'
export default {
    name: 'Storehouseroom',
    components: {
        Pagination
    },
    data() {
        return {
            departments: [],
            currentPage: 1,
            totalPage: 0,
            searchName: '', //名字
            shrtype1: '', //类
            shrtypes: [],
            types: [],
            kfList: [],
            type: ''
        }
    },
    mounted() {
        let that = this;
        layui.use('form', function () {
            var form = layui.form,
                layer = layui.layer;
            that.getPageData();
            that.loadEmnu();
            that.kfName();
            form.render('select');
            form.on('select(shr)', function (data) {
                // console.log("1", data.value)
                that.shrtype = data.value
            })
            form.on('select(shrtype)', function (data) {
                // console.log("2", data.value)
                that.searchName = data.value
            })

        });
        layui.form.render()
    },
    methods: {
        loadEmnu: function () {
            var that = this;
            $.ajax({
                url: '/sales/Sellform!getDictEmnu.action',
                type: "post",
                dataType: "json",
                data: {
                    dicttype: "22"
                },
                success: function (res) {
                    that.shrtypes = res.info
                    $.each(res.info, function (index, item) {
                        $('#sName').append("<option   value=" + item.dictkey + " > " + item.dicttext + "</option>"); // 下拉菜单里添加元素
                        //重新渲染
                        layui.form.render("select");
                    });

                }
            });
        },
        kfName() {
            let that = this;
            this.axios({
                    methods: "post",
                    url: '/produce/Storeroom!getAllStrm.action',
                    params: {type: 2}
                })
                .then(result => {
                    if (result.data.msg == "success") {
                        $.each(result.data.rooms, function (index, item) {
                            $('#shrtype').append("<option   value=" + item.name + " > " + item.name + "</option>"); // 下拉菜单里添加元素
                            //重新渲染
                            layui.form.render("select");
                        });
                        // that.kfList = result.data.rooms
                    } else {
                        that.alert("系统错误");
                    }
                })
        },
        search: function () {
            this.getPageData('search');
        },
        turnPage: function (data) {
            this.currentPage = data;
            this.getPageData();
        },
        getPageData: function (type = '') {
            let that = this;
            let loader = layui.layer.load();

            let postData = {};
            if (type != 'search') {
                postData.page = that.currentPage;
            }
            postData.searchName = that.searchName;
            postData.shrtype = that.shrtype;
            postData.type = 2;
            $.ajax({
                url: '/repository/Storehouseroom!list.action',
                type: "post",
                data: postData,
                dataType: "json",
                success: function (res) {
                    console.log(res);

                    if (res != undefined && res.rows != undefined) {
                        //分页数据赋值
                        that.currentPage = res.indexPage;
                        that.totalPage = res.pageCount;
                        that.departments = res.rows;
                    } else {
                        that.alert("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        add: function () {
            this.$parent.switchPage('AddStorehouseroomF');
        },
        edit: function (id) {
            this.$parent.gid = id;
            this.$parent.switchPage('EditStorehouseroomF');
        },
        del: function (id, likeCode) {
            let that = this;
            var checkResult = delcheckSub(id, likeCode);

            if (checkResult != 'ok') {
                that.toast(checkResult);
                return;
            }

            layer.confirm('确定要删除该数据？', function () {
                let loader = layer.load();
                $.ajax({
                    url: '/repository/Storehouseroom!del.action',
                    type: "post",
                    data: {
                        guid: id
                    },
                    dataType: "json",
                    success: function (res) {
                        var result = res.info;
                        if (result != "fail") {
                            that.toast("删除成功");
                            that.getPageData('search');
                        } else {
                            console.log(result)
                            that.toast(res.errorMsg);
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            })
        }

    }
}

function delcheckSub(id, code) {
    var result1 = "ok";
    $.ajaxSettings.async = false;
    $.post('/repository/Storehouseroom!list.action', {
        housecode: code
    }, function (result) {
        if (result.rows.length != 1) {
            result1 = "请先删除下级库间，再删该库间";

        }
    }, 'json');
    if (result1 == 'ok') {
        $.ajaxSettings.async = false;
        $.post('/repository/Storehouseroom!getWHinfo.action', {
            roomid: id
        }, function (result) {
            if ((result.info == 'false')) {
                result1 = "该库房库存信息不为‘0’，不能删除！";
            }
        }, 'json');
    }
    return result1;
}
</script>
